package com.yonyou.sunxilong.plugin;

import cn.hutool.core.convert.Convert;
import com.yonyou.iuap.ucf.common.exception.BusinessException;
import com.yonyou.sunxilong.entity.SxlCard;
import com.yonyou.sunxilong.mapper.SxlCardMapper;
import com.yonyou.ucf.mdd.ext.dao.meta.MetaDaoHelper;
import com.yonyou.ypd.bill.annotation.BillPlugin;
import com.yonyou.ypd.bill.context.YpdBillContext;
import com.yonyou.ypd.bill.plugin.AbstractBillPlugin;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.imeta.orm.schema.QueryCondition;
import org.imeta.orm.schema.QuerySchema;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

@BillPlugin(busiObj = "sxl_card", code = "sxl_card")
public class SunxilongCardPlugin extends AbstractBillPlugin {
    @Autowired
    SxlCardMapper sxlCardMapper;
    /**
     * 唯一性校验：
     * 1）项目+财务组织 唯一*
     * 2）经费卡号 唯一*
     * @param billContext
     * @throws Exception
     */
    @Override
    public void beforeSave(YpdBillContext billContext) throws Exception {
            //支持强类型转换
            SxlCard cardDO= (SxlCard) billContext.getBillDO();
            //        项目+财务组织 唯一*
            String finance_org= cardDO.getFinance_org();
            Integer i=sxlCardMapper.repeatCheckByProjectAndOrg( cardDO.getProject(),finance_org);
            if(i>0){
                throw new  Exception("已存在同 组织+项目 的单据！");
            }
            Integer j=sxlCardMapper.repeatCheckByCode(cardDO.getCode());
            if(j>0){
                throw new IllegalArgumentException("已存在相同 经费卡号 的单据！");
            }

    }

    @Override
    public void beforeDelete(YpdBillContext billContext) throws Exception {
//        beforeDeleteBusi(billContext);
        SxlCard cardDO= (SxlCard) billContext.getBillDO();
        String id = cardDO.getId();
        Map<String, Object> ecInfo = MetaDaoHelper.queryOne(
                "zyhyspring2024.zyhyspring2024.sxl_card",
                QuerySchema.create()
                        .addSelect("*")
                        .appendQueryCondition(
                                QueryCondition.name("id").eq(id)
                        )
        );

        if (ObjectUtils.isEmpty(ecInfo)) {
            throw new BusinessException("删除规则执行失败，经费卡未找到");
        }

        String project = Convert.toStr(ecInfo.get("project"));
        if (StringUtils.isBlank(project)) {
            throw new BusinessException("未获取到项目");
        }
        //校验 当前经费卡的项目，如果已经有请购单引用，该经费卡不能删除
        List<Map<String, Object>> applyOrderInfoList = MetaDaoHelper.query(
                "zyhyspring2024.zyhyspring2024.sxl_card_apply_orders",
                QuerySchema.create()
                        .addSelect("*")
                        .appendQueryCondition(
                                QueryCondition.name("project").eq(project)
                        )
        );
        if (ObjectUtils.isNotEmpty(applyOrderInfoList)) {
            throw new BusinessException("经费卡已被请购单引用，不可删除");
        }
    }
}
